package cn.rocky.pandacodegen.common.utils;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.log.NullLogChute;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Velocity工具类
 *
 * @author Rocky
 * @date 2023/06/30
 */
public class VelocityUtil {

    static {
        // 禁止输出日志
//        Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM,new NullLogChute());

    }

    /**
     * 生成
     *
     * @param context  上下文
     * @param template 模板
     * @return {@code String}
     */
    public static String generate(VelocityContext context, String template) {
        StringReader reader = new StringReader(template);
        StringWriter writer = new StringWriter();
        // 不用 vm 文件
        Velocity.evaluate(context, writer, "mystring",reader);

        try {
            writer.close();
            reader.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }
}
